#!/bin/bash
#
# AiRMP: To run ssh with prefix sshpass -p <password>
#
# Howto: 1) Save this script as ~/bin/airmp
#        2) Add env AUTO_SSH_PIN_CODE to ~/.bashrc, e.g.
#           export AUTO_SSH_PIN_CODE='ZXhwb3J0IF9QSU49Cg=='
#        3) Add alias 'rmp' to ~/.bashrc
#        4) Run alias 'rmp'
#        5) Run alias 'rmp' again
#        6) Run command 'sshpass -p $AUTO_SSH_PIN ssh ...'
#
# In short, what user has to do is to run alias 'rmp' to initialize env AUTO_SSH_PIN.
#

# XXX: Env AUTO_SSH_PIN_CODE should be included in your $HOME/.bashrc
PINCODE=${AUTO_SSH_PIN_CODE:-"ZXhwb3J0IF9QSU49Cg=="}

ENV_KEY='AUTO_SSH_PIN'

function check_env
{
    typeset out=$(env | grep -E "${ENV_KEY}=")
    echo $out
}

kv01=$(check_env)
F=/tmp/.${ENV_KEY}
echo ${PINCODE} | base64 -d | sed -e 's/*/J/g' -e 's/#//g' -e "s%_PIN%${ENV_KEY}%" > $F
kv02=$(source $F && check_env)
if [[ $kv01 != $kv02 ]]; then
    echo ">>> File $(basename $F) is ready."
    echo ">>> Please run alias: rmp"
else
    rm -f $F
    echo ">>> Env ${ENV_KEY} is set."
    echo ">>> Please run comand: sshpass -P \$${ENV_KEY} ssh ..."
    echo ">>> OR     run  alias: ppai"
fi

#
# == Include these aliases in your $HOME/.bashrc ======================================================================
# export AUTO_SSH_PIN_CODE='ZXhwb3J0IF9QSU49Cg==' # XXX: It's fake like: export _PIN=
# alias rmp='F_RMP=/tmp/.AUTO_SSH_PIN;source $F_RMP > /dev/null 2>&1 && (echo OK && rm -f $F_RMP) || airmp;unset F_RMP'
# alias ppai='sshpass -p $AUTO_SSH_PIN ssh ai'
# =====================================================================================================================
#
